package com.hu.springcloud.service.impl;

import com.hu.springcloud.dao.OrderDao;
import com.hu.springcloud.domain.Order;
import com.hu.springcloud.service.AccountService;
import com.hu.springcloud.service.OrderService;
import com.hu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        //1.新建订单
        log.info("***开始新建订单***");
        orderDao.create(order);

        //2.扣减库存
        log.info("***订单微服务开始调用库存，扣减数量***");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("***订单微服务做扣减结束***");

        //3.扣减账户
        log.info("***订单微服务开始调用账户，扣减余额***");
        accountService.decrease(order.getUserId(),order.getMoney());

        //4.修改订单状态，从0到1，1表示完成
        log.info("***修改订单状态***");
        orderDao.update(order.getUserId(),0);
        log.info("***修改订单状态完成***");

        log.info("***下订单成功，O(∩_∩)O哈哈~***");
    }
}
